import { login, logout, getInfo, loginOldAPI } from '@/api/user'
import {
  getToken, setToken, removeToken, setPermission,
  setStorageUserInfo, getStorageUserInfo, removePermission, removeStorageUserInfo
} from '@/utils/auth'
import { resetRouter } from '@/router'
import store from '@/store'
import router from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(), // token
    userInfo: getStorageUserInfo(), // 用户信息
    name: '',
    avatar: '',
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },

  // 将token存储到state中
  SET_TOKEN: (state, token) => {
    state.token = token
    // 持久化存储token
    setToken(token)
  },

  // 将用户信息存储到state中
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
    // 持久化存储用户信息
    setStorageUserInfo(userInfo)
  }
}

const actions = {
  // 登录-》获取token
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(async response => {
        const { data } = response
        // 存储token
        commit('SET_TOKEN', data.token)

        data.userInfo.token = data.token

        // 存储用户信息
        commit('SET_USERINFO', data.userInfo)
        
        // 发送网络请求 获取动态路由
        await store.dispatch('user/getUserInfo')

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 登录-》获取用户信息&路由信息
  getUserInfo({ commit, state }) {
    const param = { action: 'tree', userno: getStorageUserInfo().userno }

    return new Promise(async(resolve, reject) => {
      const result = await getInfo(param)

      // 将左侧菜单栏/动态路由的数据存储到本地
      setPermission(result.data.tree)

      resolve(result)// Promise状态为成功所返回的数据
    }, error => {
      reject(error)
    })
  },

  // 退出登录
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 暂时不需要调用接口退出登录
      /* const data = {
        action: 'logout',
        userno: getStorageUserInfo().userno // 用户名称
      }

      logout(data).then((res) => {
        if (res.code == 0) {
          // 清除路由
          resetRouter()

          // 清除本地存储中所有的数据 token、UserInfo....
          localStorage.clear()

          // 清除state中的数据
          commit('RESET_STATE')

          // 将路由状态修改为false 下次登录刷新路由
          router.options.isAddDynamicMenuRoutes = false

          resolve()
        }
      }).catch(error => {
        reject(error)
      }) */

      // 清除路由
      resetRouter()

      // 清除本地存储中的数据 token、UserInfo....
      removeToken()
      removePermission()
      removeStorageUserInfo()


      // 清除state中的数据
      commit('RESET_STATE')

      // 将路由状态修改为false 下次登录刷新路由
      router.options.isAddDynamicMenuRoutes = false

      resolve()
    })
  },

  // 登录旧版通用版
  login_old({ commit,state }) {
    return new Promise(async (resolve, reject) => {
      const userno = {
        userno: state.userInfo.userno||'',
        action:'loginAPI'
      }
      await loginOldAPI(userno).then(async res => { 
       console.log('旧版登录',res);
      resolve(res)
      }, err => { 
        reject(err)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first

      commit('RESET_STATE')

      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

